package com.org.csu.controller;

import com.org.csu.common.AccountResponse;
import com.org.csu.pojo.KeywordDTO;
import com.org.csu.pojo.Record;
import com.org.csu.pojo.User;
import com.org.csu.pojo.UserDTO;
import com.org.csu.service.accountService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/user")
@Validated
@CrossOrigin(origins = "http://localhost:5173")
public class accountController {
    @Autowired
    private accountService accountService;
    //登录
    @PostMapping("/login")
    public AccountResponse<String> CheckUser(@RequestBody Map<String, String> request, HttpServletRequest httpRequest) {
        String username = request.get("userName");
        String password = request.get("password");
        String msg = accountService.isRightOfAccount(username, password);
        Integer userId = accountService.getUserIdbyName(username);
        System.out.println("userId: " + userId);
        if (msg.equals("登录成功")) {
            return new AccountResponse(0, "登录成功", userId);
        } else {
            return new AccountResponse(1, msg);
        }
    }
    @PostMapping("/register")
    public AccountResponse<String> register(@RequestBody Map<String, String> requestBody, HttpServletRequest httpRequest) {
        String username = requestBody.get("userName");
        String email = requestBody.get("email");
        String password = requestBody.get("password");

        // 输入验证
        if (username == null || username.isEmpty()) {
            return new AccountResponse(1,"用户名不能为空");
        }
        if (password == null || password.isEmpty()) {
            return new AccountResponse(1,"密码不能为空");
        }

        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        String dateString = now.format(formatter);
        user.setDate(dateString);
        System.out.println(user.getDate());
        user.setPermission(0);
        String msg = accountService.setUserInfo(user);
        if (msg.equals("注册成功")) {
            return new AccountResponse(0,"注册成功,请登录");
        } else {
            return new AccountResponse(1,msg); //  返回更具体的错误信息
        }
    }
    @GetMapping("/userInfo")
    public AccountResponse<UserDTO> getUserInfo(@RequestParam("userId") Integer userId){
        User user = accountService.getuserInfoByUserId(userId);
        List<Record> records = accountService.getrecordInfoByUserId(userId);
        UserDTO userDTO = new UserDTO(user.getUsername(), user.getPassword(), user.getPermission(),user.getDate(),user.getEmail(),records);
        if (user != null)
            return new AccountResponse(0,"获取成功",userDTO);
        else
            return new AccountResponse(1,"用户名错误！");
    }

    @PutMapping("")
    public AccountResponse<String> recharge(@RequestBody Map<String, Integer> requestBody, HttpServletRequest httpRequest) {
        Integer userId = requestBody.get("userId");
        Integer permission = requestBody.get("permission");

        if (userId == null || permission == null) {
            return new AccountResponse(1,"userId 和 permission 都是必需的。");
        }

        String msg = accountService.recharge(userId, permission);
        return msg.equals("充值成功") ? new AccountResponse(0,msg) : new AccountResponse(1,msg);
    }
    @GetMapping("/keyword")
    public AccountResponse<KeywordDTO> getSearch(@RequestParam("userId")Integer userId, @RequestParam("keyword")String keyword,
                                                @RequestParam("date")String date){
        KeywordDTO keywordDTO = accountService.getinformationsByrecordId(accountService.getrecordIdByinformations(userId,keyword,date));
        if (keywordDTO != null)
            return new AccountResponse(0,"culpa Duis cillum reprehenderit et",keywordDTO);
        else
            return new AccountResponse(1,"无浏览记录");
    }
}